package com.mybatis.basic.service.impl;

import java.util.List;
import java.util.concurrent.TimeUnit;
import javax.annotation.Resource;
import org.springframework.dao.DataAccessException;
import org.springframework.data.redis.connection.RedisConnection;
import org.springframework.data.redis.core.RedisCallback;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.mybatis.basic.service.RedisService;

@Service
public class RedisServiceImpl implements RedisService {

	@Resource(name="redisTemplate")
	private RedisTemplate<String, Object> redisTemplate;

	@Override
	public void set(String key, Object obj, long time, Class<?> clazz) throws Exception {
		String jsonValue = "";
		//判断是不是List对象
		if(clazz.isAssignableFrom(List.class)) {
			jsonValue = JSONArray.toJSONString(obj);
		}else {
			jsonValue = JSONObject.toJSONString(obj);
		}
		redisTemplate.opsForValue().set(key, jsonValue, time, TimeUnit.SECONDS);
	}

	@Override
	public void set(String name, String key, Object value, long time, Class<?> clazz) throws Exception {
		String jsonValue = "";
		//判断是不是List对象
		if(clazz.isAssignableFrom(List.class)) {
			jsonValue = JSONArray.toJSONString(value);
		}else {
			jsonValue = JSONObject.toJSONString(value);
		}
		redisTemplate.opsForHash().put(name, key, jsonValue);
		redisTemplate.expire(name, time, TimeUnit.SECONDS);
	}

	@Override
	public Object getObject(String key, Class<?> clazz) throws Exception {
		Object obj = redisTemplate.opsForValue().get(key);
		if(null!=obj) {
			String jsonValue = obj.toString();
			if(clazz.isAssignableFrom(List.class)) {
				obj = JSONArray.parseObject(jsonValue, clazz);
			}else {
				obj = JSONObject.parseObject(jsonValue, clazz);
			}
			return obj;
		}
		return null;
	}

	@Override
	public Object getObject(String name, String key, Class<?> clazz) throws Exception {
		Object obj = redisTemplate.opsForHash().get(name, key);
		if(null!=obj) {
			String jsonValue = obj.toString();
			if(clazz.isAssignableFrom(List.class)) {
				obj = JSONArray.parseObject(jsonValue, clazz);
			}else {
				obj = JSONObject.parseObject(jsonValue, clazz);
			}
			return obj;
		}
		return null;
	}

	@Override
	public Integer getInt(String key) throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public String getString(String key) throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Double getDouble(String key) throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void del(String key) throws Exception {
		redisTemplate.delete(key);
	}

	@Override
	public void delByHash(String group) {
		redisTemplate.delete(group);
	}

	@Override
	public void delByHash(String group, String key) throws Exception {
		redisTemplate.opsForHash().delete(group, key);
	}

	@Override
	public boolean clearDb() {
		redisTemplate.execute(new RedisCallback<Object>() {
			@Override
			public Object doInRedis(RedisConnection conn) throws DataAccessException {
				conn.flushDb();
				return true;
			}
		});
		return false;
	}
	
}
